# -*- coding: utf-8 -*-
# @Time    : 2024/2/23  11:21
# @Author  : zhanghao
# @FileName: login.py
# @Software: PyCharm
"""
    Description:
        
"""
from DrissionPage import ChromiumPage

from logger import Logger
from config import Config

import time
import random


class Login:
    def __init__(self):
        pass

    logger = Logger.get()
    config = Config()

    url = 'https://oa.hzgjbbg.cn/#/login?redirect=/'

    def processSign(self):
        self.config.initConfig()

        randomTime = random.randint(80, 200)
        self.logger.info('start sign... sleep ' + str(randomTime) + 's')
        time.sleep(randomTime)

        # 登陆签到
        name_list = self.config.names
        password_list = self.config.passwords

        flag = len(name_list) > 1
        for idx in range(len(name_list)):
            self.autoSign(flag, name_list[idx], password_list[idx])

    def processSignOut(self):
        self.config.initConfig()

        randomTime = random.randint(80, 200)
        self.logger.info('start sign out... sleep ' + str(randomTime) + 's')
        time.sleep(randomTime)

        # 登陆签到
        name_list = self.config.names
        password_list = self.config.passwords

        for idx in range(len(name_list)):
            self.autoSignOut(name_list[idx], password_list[idx])

        self.logout()

    def autoSign(self, flag, name, password):
        self.logger.info('start login user:' + name)

        page = ChromiumPage()
        try:
            page.get(self.url)

            input_tag = page.eles('@class=n-input__input-el')
            input_tag[0].input(name)
            input_tag[1].input(password)

            page.ele(' 登 录 ').click()
            time.sleep(3)

            time_records = page.eles('@class=timeNow')
            if time_records and len(time_records) > 1 and time_records[1].raw_text:
                self.logger.info('login success, user:' + name + ' login time:' + time_records[1].raw_text)
            else:
                self.logger.info('login failed, user:' + name)

            time.sleep(2)
            page.set.cookies.remove('PANO_SESSION', url='https://oa.hzgjbbg.cn')
            page.set.cookies.remove('token', url='https://oa.hzgjbbg.cn')
            page.refresh()
            time.sleep(2)
        finally:
            page.close()

    def autoSignOut(self, name, password):
        self.logger.info('start sign out user:' + name)

        page = ChromiumPage()
        page.get(self.url)

        try:
            input_tag = page.eles('@class=n-input__input-el')
            input_tag[0].input(name)
            input_tag[1].input(password)

            page.ele(' 登 录 ').click()
            time.sleep(3)

            time_records = page.eles('@class=timeNow')
            if time_records and len(time_records) > 1 and time_records[1].raw_text:
                self.logger.info('login success, user:' + name + ' login time:' + time_records[1].raw_text)
            else:
                self.logger.info('login failed, user:' + name)

            page.ele('签出').click()
            time.sleep(2)

            logout = page.ele('@class=n-button n-button--primary-type n-button--medium-type')
            logout.click()

            time.sleep(2)
            page.set.cookies.remove('PANO_SESSION', url='https://oa.hzgjbbg.cn')
            page.set.cookies.remove('token', url='https://oa.hzgjbbg.cn')
            page.refresh()
            time.sleep(2)

        finally:
            page.close()
        self.logger.info('start sign out success:' + name)

    def logout(self):
        page = ChromiumPage()
        page.get(self.url)
        page.set.cookies.remove('PANO_SESSION', url='https://oa.hzgjbbg.cn')
        page.set.cookies.remove('token', url='https://oa.hzgjbbg.cn')
        page.get(self.url)

        time.sleep(2)
        page.close()
